// pages/componentSm/register/register.js
import WxValidate from '../../../utils/WxValidate.js';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    
  },
  options: {
    addGlobalClass: true,
  },

  /**
   * 组件的初始数据
   */
  data: {
    countDownText:{
      "cdTime": 10 , //倒计时事件(秒)
      "title":"获取验证码",  //文字
      "cdTimer": null,  //倒计时定时器
      "state":true,  //是否可以点击
    },
    form:{
      userName: "",  //用户名
      pwd: "",     //密码
      pwdAgain: "",  //确认密码
      email: "",    //邮箱
      company: "",  //公司
      userPhone: "",   //手机号
      verificationCode: "",  //验证码 
    }
  },
  lifetimes: {
    attached() {
      // 在组件实例进入页面节点树时执行
      this.initValidate()//验证规则函数
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //报错
    showModal(error){
      wx.showModal({
        content:error.msg,
        showCancel:false,
      })
    },
    //验证函数
    initValidate(){
      const rules = {
        userName:{
          required:true,
          rangelength:[2,20]
        },
        pwd:{
          required:true,
          rangelength: [6, 16]
        },
        pwdAgain:{
          required:true,
          equalTo:'pwd'
        },
        email:{
          required:true,
          email:true
        },
        company:{
          required:true,
          minlength:4
        },
        userPhone:{
          required:true,
          tel:true
        },
        verificationCode:{
          required:true,
          digits:true,
          minlength:6
        }
      }
      const message = {
        userName: {
          required: '请填写用户名',
          rangelength: '长度应为2~20个字符'
        },
        pwd: {
          required: '请填写密码',
          rangelength: '密码长度应为6~16个字符'
        },
        pwdAgain: {
          required: '请再次填写密码',
          equalTo: '两次填写的密码不一致'
        },
        email: {
          required: '请填写邮箱',
          email: '邮箱错误'
        },
        company: {
          required: '请填写公司名',
          minlength: '请填写正确的公司名'
        },
        userPhone: {
          required: '请填写手机号',
          tel: '请填写正确的手机号码'
        },
        verificationCode: {
          required: '请填写验证码',
          digits: '验证码只能为数字',
          minlength: '请填写正确的验证码'
        }
      }
      this.WxValidate = new WxValidate(rules,message);
    },
    //调用验证函数
    formSubmit:function(e){
      const params = e.detail.value

      //校验表单
      if(!this.WxValidate.checkForm(params)){
        const error = this.WxValidate.errorList[0]
        this.showModal(error)
        return false
      }
      this.showModal({
        msg:'提交成功'
      })
    },
    //获取验证码的方法
    getCode(){
      this.countDown();
    },
    //验证码倒计时
    countDown(){
      let { cdTime,title,cdTimer,state } = this.data.countDownText;
      
      if (cdTime == 0){
        cdTime = 10;
        state = true;
        let countDownText = Object.assign({}, countDownText, { title}, { cdTime }, { cdTimer }, { state });
        this.setData({
          countDownText
        });
        return;
      }

      cdTimer = setTimeout(() => {
        clearTimeout(cdTimer);
        cdTime--;
        state = false;

        let countDownText = Object.assign({}, countDownText, { title }, { cdTime }, { cdTimer }, { state });
        this.setData({
          countDownText
        });

        this.countDown();
      },1000);
    }
  }
})
